﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace ExplodingCow.Basilisk.Logic
{
    /// <summary>
    /// Data type for the loading of previously saved xml files containing shopping list items and their data
    /// </summary>
    public class ListLoader
    {
        /// <summary>
        /// private instance of ListLoader, Singleton
        /// </summary>
        static ListLoader instance;

        /// <summary>
        /// private singleton constructor
        /// </summary>
        private ListLoader()
        {

        }

        /// <summary>
        /// Get the static instance of a ListLoader
        /// </summary>
        /// <returns>static instance of ListLoader</returns>
        public static ListLoader GetListLoader()
        {
            if (instance == null)
            {
                instance = new ListLoader();
            }
            return instance;
        }

        public List<Item> LoadList(string basaliskSavedListToLoad)
        {
            bool loadSuccessful = false;
            List<Item> itemsToLoad = new List<Item>();
            //create logic for loading in a specified file
            XDocument itemListToLoad = XDocument.Load(basaliskSavedListToLoad);

            //load was successful or not
            //loadSuccessful = parseXDocument(itemListToLoad);
            //true
            //if (loadSuccessful)
            //{
            //    itemsToLoad = loadParsedXDocument(loadSuccessful, itemListToLoad);
            //}
            //else //false
            //{
            //    //set itemsToLoad as a new "Empty" list
            //    itemsToLoad = new List<Item>();
            //}
            itemsToLoad = loadParsedXDocument(loadSuccessful, itemListToLoad);
            return itemsToLoad;
        }
        /// <summary>
        /// Read the data and make sure that it is in a usable format to the basalisk system
        /// </summary>
        /// <returns></returns>
        private bool parseXDocument(XDocument xDocumentToParse)
        {

            bool successful = false;
            //check to see if everything came back successfully from the parse
            return successful;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="successful"></param>
        /// <param name="parsedXDocument"></param>
        /// <returns></returns>
        //private List<Item> loadParsedXDocument(bool successful, XDocument parsedXDocument)
        //{
        //    List<Item> loadedItems = new List<Item>();

        //    #region old code
        //    //get the data from the XDocument object
        //    IEnumerable<XElement> xmlItemsElements;
        //    //string list = parsedXDocument.Root.Element("List").Value;
        //    //string items = parsedXDocument.Root.Element("Items").Value;
        //    xmlItemsElements = parsedXDocument.Root.Element("Items").Elements();
            
        //    //string itemName = parsedXDocument.Root.Element("Item").Element("Name").Value;
        //    //the number of items that are contained in the xml document
        //    int itemCount = parsedXDocument.Root.Element("Item").Elements().Count();

        //    string itemName = parsedXDocument.Root.Element("Item").Element("Name").Value;
        //    string itemDescription = parsedXDocument.Root.Element("Item").Element("Description").Value;
        //    string itemCategory = parsedXDocument.Root.Element("Item").Element("Category").Value;
        //    string itemQuanitity = parsedXDocument.Root.Element("Item").Element("Quantity").Value;
        //    string itemCost = parsedXDocument.Root.Element("Item").Element("Cost").Value;
        //    string itemStoreLocation = parsedXDocument.Root.Element("Item").Element("StoreLocation").Value;

        //    IEnumerable<Item> items = xmlItemsElements.Select(i => new Item());
        //    #endregion old code

        //    return loadedItems;
        //}
        /// <summary>
        /// private: loads in the XDocument after parsing reads the data and returns as a List of type Item
        /// </summary>
        /// <param name="successful">was the file read successful</param>
        /// <param name="parsedXDocument">The current XDocument to read</param>
        /// <returns>A list of items containing the data from the loaded file</returns>
        private List<Item> loadParsedXDocument(bool successful, XDocument parsedXDocument)
        {
            IEnumerable<XElement> itemElements = new List<XElement>();
            // XElement rootElement = parsedXDocument.Root;

            List<Item> listItems = new List<Item>();

            itemElements = parsedXDocument.Root.Element("items").Elements("item");
            Item item;
            foreach (XElement elem in itemElements)
            {
                item = new Item()
                {
                    Name = elem.Element("name").Value,
                    Cost = decimal.Parse(elem.Element("cost").Value),
                    Quantity = int.Parse(elem.Element("quantity").Value),
                    Description = elem.Element("description").Value,
                    Category = (Category)Enum.Parse(typeof(Category), elem.Element("category").Value, true),
                    StoreLocation = new StoreLocation()
                    {
                        Name = elem.Element("location").Element("name").Value,
                        Address = elem.Element("location").Element("address").Value,
                        City = elem.Element("location").Element("city").Value,
                        State = elem.Element("location").Element("state").Value,
                        Zip = elem.Element("location").Element("postal").Value
                    },
                };
                listItems.Add(item);
            }
            //debug purposes
            int othercount = listItems.Count;
            return listItems;

        }
        /// <summary>
        /// get the data locally from the "IEnumerable of Item"
        /// </summary>
        /// <param name="dataItems">IEnumerable of Items to read data from</param>
        /// <returns>a List of type Item</returns>
        private List<Item> getXmlDataItems(IEnumerable<Item> dataItems)
        {
            List<Item> dataItemsToBeReturned = new List<Item>();
            IEnumerable<XNode> descendantNodes;
            Item tempItem;
            //figure out logic from here, params may change too
            foreach (Item item in dataItems)
            {
                tempItem = new Item();
                
                //set properties to that of the object passed in
                tempItem.Copy(item);
                dataItemsToBeReturned.Add(tempItem);
            }
            return dataItemsToBeReturned;
        }
    }
}
